package com.admin.controller;

import com.admin.entity.Delivery;
import com.admin.service.DeliveryService;
import com.admin.service.GoodsService;
import com.admin.entity.Goods;
import com.admin.entity.Storage;
import com.admin.service.StorageService;
import com.admin.sys.base.module.extend.web.BaseController;
import com.admin.sys.base.entity.ResultInfo;
import com.admin.sys.utils.admin.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
*【物品信息】页面接口
*/
@RestController
@RequestMapping("/admin/goods")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private StorageService storageService;
    @Autowired
    private DeliveryService deliveryService;

    /**
    * 【物品信息】根据条件分页查询
    * @param page
    * @param goods
    * @return
    */
    @RequestMapping("/getPage")
    public ResultInfo getPage(Page<Goods> page, Goods goods) {
        IPage<Goods> iPage = goodsService.getPage(page, goods);
        return ResultInfo.ok("获取分页成功", iPage);
    }

    /**
    * 【物品信息】根据条件查询
    * @param goods
    * @return
    */
    @RequestMapping("/getList")
    public ResultInfo getList(Goods goods) {
        List<Goods> list = goodsService.getList(goods);
        return ResultInfo.ok("获取列表成功", list);
    }

    /**
    * 【物品信息】根据对象查询
    * @param goods
    * @return
    */
    @RequestMapping("/get")
    public ResultInfo get(Goods goods) {
        Goods entity = goodsService.get(goods);
        return ResultInfo.ok("获取对象成功", entity);
    }

    /**
    * 【物品信息】提交(新增或修改)
    * @param goods
    * @return
    */
    @RequestMapping("/sub")
    public ResultInfo insert(Goods goods) {
        if (StringUtils.isEmpty(goods.getId())) { //新增
            goodsService.insert(goods);
        } else {//修改
            goodsService.update(goods);
        }
        return ResultInfo.ok("提交成功!");
    }

    /**
    * 【物品信息】删除
    * @param id
    * @return
    */
    @RequestMapping("/delete")
    public ResultInfo delete(String id) {
        Storage storage = new Storage();
        storage.setGoodsId(id);
        List<Storage> storageList = storageService.getList(storage);
        if (storageList.size() > 0) {
            return ResultInfo.error("该物品存在入库记录不能删除！");
        }
        Delivery delivery = new Delivery();
        delivery.setGoodsId(id);
        List<Delivery> deliveryList = deliveryService.getList(delivery);
        if (deliveryList.size() > 0) {
            return ResultInfo.error("该物品存在出库记录不能删除！");
        }
        goodsService.delete(id);
        return ResultInfo.ok("删除成功!");
    }

    /**
    * 【物品信息】批量删除
    * @param ids
    * @return
    */
    @RequestMapping("/delAll")
    public ResultInfo delAll(String[] ids) {
        for (String id : ids) {
            Storage storage = new Storage();
            storage.setGoodsId(id);
            List<Storage> storageList = storageService.getList(storage);
            if (storageList.size() > 0) {
                return ResultInfo.error("该物品存在入库记录不能删除！");
            }
            Delivery delivery = new Delivery();
            delivery.setGoodsId(id);
            List<Delivery> deliveryList = deliveryService.getList(delivery);
            if (deliveryList.size() > 0) {
                return ResultInfo.error("该物品存在出库记录不能删除！");
            }
            goodsService.delete(id);

        }
        return ResultInfo.ok("删除成功!");
    }



}



